package com.ymatou.iapi.prodstock.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.AgentPriceDto;
import com.shop2cn.iapi.agentproducts.parameter.CatalogDto;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.ymatou.iapi.godmch.parameter.AgentTypeVo;
import com.ymatou.iapi.godmch.parameter.listagentTypeBean;
import com.ymatou.iapi.liveseller.parameter.ActivityProductDto;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.LiveActivityDto;
import com.ymatou.iapi.prodstock.parameter.OrderReturnCatalogDto;
import com.ymatou.iapi.prodstock.parameter.OrderReturnCatalogDtoV2;
import com.ymatou.iapi.prodstock.parameter.PCatalogListDto;
import com.ymatou.iapi.prodstock.parameter.PreOrderReduceCatalogDto;
import com.ymatou.iapi.prodstock.parameter.ReturnProductStockForOrderBean;
import com.ymatou.iapi.prodstock.parameter.UpdateProductActivityStockTimeBean;
import com.ymatou.iapi.prodstock.service.ReturnProductStockForOrderCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmongosync.service.EsCall;
import com.ymatou.iapi.productmongosync.service.MongocrudCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.database.RedisDriver;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.ProdStockCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ProductPriceCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.mongo.jproduct.ActivityProductsWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductActivityStockWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

import cn.ymatou.activityadmin.service.CloseProductActivityCall;

public class Ts_ReturnProductStockForOrder {
	// default data
	private static String sellerName = EnvSetup.getData("autotest_sellerName1");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static int sellerId1 = Integer.parseInt(EnvSetup.getData("productpriceSellerId"));
	private static int sellerId2 = Integer.parseInt(EnvSetup.getData("productsellerid"));

	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
	private static ActivityProductsWapper activityProductsWapper = new ActivityProductsWapper();
	private static YmtProductActivityStockWapper ymtProductActivityStockWapper = new YmtProductActivityStockWapper();
	private static List<String> productlist;
	private static ReturnProductStockForOrderCall returnProductStockForOrderCall = new ReturnProductStockForOrderCall();
    private static YmtCatalogsIWapper CIWapper=new YmtCatalogsIWapper();
    private static ReturnProductStockForOrderBean returnProductStockForOrderBean = new ReturnProductStockForOrderBean();
	private static IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();

	//渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	//渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	//渠道商->代理商2
	private static int agentUserId2 = AgentUsers.agentUserId2;
	private static String agentUserName2 = "NickautoTest";

	//vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	//vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	//vip渠道->代理->代理
	private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
	private static String vipAgentUserName2 = "NickautoTest";
	@BeforeClass
	public static void setUp() throws ClientProtocolException, IOException, Exception {
		Logger.createResultFile("订单取消返还库存");
		
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException, Exception {
		productlist=new ArrayList<>();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(returnProductStockForOrderCall.getOpurl());
		
	}

	@After
	public void caseDown() throws ClientProtocolException, IOException, Exception {
		Logger.debug("下架商品");
		ProductManagerCallServiceV2.updateActivityStock0(productlist);
		ProductManagerCallServiceV2.batchSetProductOffSale(productlist,sellerId);
		Logger.end();
	}     

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_001() {
		Logger.start(true, "普通现货商品,返库存");
		try{
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存
			Integer catalogNum = 10000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			productlist.add(productId);
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int stockNum = 2;
			
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, stockNum, 0, true);
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum-stockNum,0);
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum+1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum+1, 0, true);
			
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew1, catalogNum+1, 0, true);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_002() {
		Logger.start(true, "普通现货商品,已删除规格不能扣减库存");
		try{
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存
			Integer catalogNum = 10000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			productlist.add(productId);
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int stockNum = 2;
			 //已删除
			CIWapper.updateiActionByCatalogId(-1, ymtCatalog.getsCatalogId());
			//同步monogo、es
			EsCall esCall=new EsCall();
			MongocrudCall mongoCall=new MongocrudCall();
			esCall.callAndGetReturnData(MapUtil.hashMap("ProductId",productId,"ActionType","ModifyProduct","TransactionId",TestDataManager.getRandomIntNum(5)));
			mongoCall.callAndGetReturnData(MapUtil.hashMap("ProductId",productId,"ActionType","ModifyProduct","TransactionId",TestDataManager.getRandomIntNum(5)));
			
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, stockNum, 0, false);
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum,0,false);
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum, 0, false);
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew1, catalogNum, 0, false);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_003() {
		Logger.start(true, "普通现货商品,删除规格返库存");
		try{
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存
			Integer catalogNum = 10000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			productlist.add(productId);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int stockNum = 2;
			
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, stockNum, 0, true);
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum-stockNum,0);
			
			
			String catalogsId = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).get(0).getsCatalogId();
			//modify product and on sale 
//			Map<String, Object> updateMap = MapUtil.hashMap("LoginID", sellerName, "UserID", sellerId,
//															"Product->sProductId",productId,"ProductID",productId,
//															"Product->OnSaleType",0,	// 在售类型    Old = -1,  Immediately = 0,    SpecialTime = 1,   OffSale = 2,
//															"Product->Catalogs[2]->CatalogId",catalogsId);
//			//1
//			String fileName = "changenewproductinfocalltpl_001.json";
//			new ChangeNewProductInfoCall().callAndGetReturnData(fileName,updateMap);
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum+1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum+1, 0, true);
			
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew1, catalogNum+1, 0, true);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_004() {
		Logger.start(true, "活动商品,没有限购,返库存");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存
			Integer catalogNum = 10000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			int actNum = 10;
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品:" + productId);
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, actNum,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			ymtProductActivityStockWapper.updateStartTimeByProductId(productId, YMTDateUtil.getBeforeOrNextSecond(-300, YMTDateUtil.YMDHMS));
			
			productlist.add(productId);
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int stockNum = 2;
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			// 扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,
					sellerId, aid, stockNum, 0, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum-actNum, actNum-stockNum);
			
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum+1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew,  catalogNum-actNum, actNum+1, true);
			
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew1,  catalogNum-actNum, actNum+1, true);
		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_005() {
		Logger.start(true, "活动商品,活动限购,扣减库存,返库存,扣库存");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存
			Integer catalogNum = 10000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			Integer actNum = 10;
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建商品:" + productId);
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, actNum,"10");
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			ymtProductActivityStockWapper.updateStartTimeByProductId(productId, YMTDateUtil.getBeforeOrNextSecond(-300, YMTDateUtil.YMDHMS));
			String catalogId = catalogs.get(0);
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(activityId, 0, 3);
			int stockNum=3;
			// 扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, activityId, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto,  catalogNum-actNum, actNum-stockNum);
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(activityId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew,  catalogNum-actNum, actNum, true);
			
			
			//该用户再下单
			// 扣库存
			PCatalogListDto catalogListDto3 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, activityId, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto3,  catalogNum-actNum, actNum-stockNum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_006() {
		Logger.start(true, "活动商品,活动商品限购,扣减库存,返库存,扣库存");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			productlist.add(productId);
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(aid, 1, 3);
			
			// 扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum, actNum, true);
			
			
			//该用户再下单
			// 扣库存
			PCatalogListDto catalogListDto3 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum, actNum-stockNum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_007() {
		Logger.start(true, "活动商品,扣减库存,活动结束返库存");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			productlist.add(productId);
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
		
			// 扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 0,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			//结束活动
			int inaid = new YmtProductsInActivityWapper().selectByProductIdAndActivityId(productId, aid).getIproductinactivityid();
			new CloseProductActivityCall().closeProductInActivity(Arrays.asList(inaid));
			YMTDateUtil.waitTime(5);
			int catalogNumNew = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogId).getiNum();
			
			//反库存 返共享库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			
			//check
			catalogStockListDtoNew.setActivityId(0);
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNumNew+stockNum, 0, true);

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_008() {
		Logger.start(true, "普通FBX商品,扣减库存,反库存");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存
			Integer catalogNum = 10000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			
			productlist.add(productId);
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int stockNum = 2;
			
			//扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, stockNum, 2, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum-stockNum,0);
			

			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//20180428 fbx去除改造 JIRACONN-20890
//			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum, 0, true);
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto,true,true);
			//check
//			ProdStockCallService.checkReturnStock(catalogStockListDtoNew1, catalogNum, 0, true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_009() {
		Logger.start(true, "FBX活动商品,没有限购,扣库存,返库存");
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			productlist.add(productId);
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, aid, stockNum, 2, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum,actNum-stockNum);
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum, actNum, true);
			
			//反库存 幂等
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew1 = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto,true,true);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew1, catalogNum, actNum, true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_010() {
		Logger.start(true, "FBX活动商品,活动限购,扣减库存,反库存 ");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			productlist.add(productId);
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(aid, 0, 3);
			
			//扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 2,buyerId, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum, actNum, true);
			
			//该用户再下单
			// 扣库存
			PCatalogListDto catalogListDto3 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 2,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum, actNum-stockNum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_011() {
		Logger.start(true, "FBX活动商品,活动商品限购,扣减库存,反库存 ");
		try {
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			productlist.add(productId);
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(aid, 1, 3);
			
			//扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 2,buyerId, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum, actNum-stockNum);
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum, actNum, true);
			
			//该用户再下单
			// 扣库存
			PCatalogListDto catalogListDto3 = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId,sellerId, aid, stockNum, 2,buyerId, true);
			// check
			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum, actNum-stockNum);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_012() {
		Logger.start(true, "FBX活动商品,没有限购,扣库存,活动结束,返库存");
		try {
			String productId = ProductPriceCallService.getProdPriceWithActivity_NewBuyerAndNoIsolation(sellerId).getProductId();
			productlist.add(productId);
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			new YmtCatalogsIWapper().updateCatalogTypeByProductId(productId, 2);
			
			YmtCatalogsI ymtCatalog = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			String catalogId = ymtCatalog.getsCatalogId();
			int catalogNum = ymtCatalog.getiNum();
			int stockNum = 2;
			
			int actNum = ymtProductActivityStockWapper.selectByCatalogId(catalogId).getIstocknum();
			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			
			//扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, aid, stockNum, 2, true);
			//check
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum,actNum-stockNum);
			
			//结束活动
			int inaid = new YmtProductsInActivityWapper().selectByProductIdAndActivityId(productId, aid).getIproductinactivityid();
			new CloseProductActivityCall().closeProductInActivity(Arrays.asList(inaid));
			YMTDateUtil.waitTime(5);
			int catalogNumNew = ymtCatalogsIWapper.selectCatalogsByCatalogId(catalogId).getiNum();
			
			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(catalogId);
			catalogStockListDto.setSellerId(sellerId);
			catalogStockListDto.setStockNum(stockNum);
			catalogStockListDto.setActivityId(aid);
			catalogStockListDto.setCatalogType(2);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
		
			//check
			catalogStockListDtoNew.setActivityId(0);
			catalogStockListDtoNew.setCatalogType(0);
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNumNew+stockNum, 0, true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	
	
	@Test
	@Category({ P1.class })
	@TestCase
	@org.junit.Ignore
	public void Tc_ReturnProductStockForOrder_013() {
		Logger.start(true, "活动商品,活动限购,扣减库存,返库存,多个订单");
		try {

			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			//商品A
			String productId1 = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			String price1 = "9.0";
			Date beginTime = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(-3600));
			Date endTime = YMTDateUtil.parseYMDHMSDate(YMTDateUtil.getBeforeOrNextSecond(3600));
			int aid = ProductPriceCallService.ApplyHighActivity(price1,productId1,beginTime,endTime).getActivityId();
			YmtCatalogsI ymtCatalog1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId1).get(0);
			String catalogId1 = ymtCatalog1.getsCatalogId();
			int reduceNum1 = 1;
			int actNum1 = ymtProductActivityStockWapper.selectByCatalogId(catalogId1).getIstocknum();
			//商品B
			String productId2 = ProductManagerCallServiceV3.AddSellerProduct(addpro);
			String price2 = "9.0";
			int aid2 = ProductPriceCallService.ApplyHighActivity(price2,productId2,beginTime,endTime).getActivityId();
			YmtCatalogsI ymtCatalog2 = ymtCatalogsIWapper.selectCatalogsByProductId(productId2).get(0);
			String catalogId2 = ymtCatalog2.getsCatalogId();
			int reduceNum2 = 1;
			int actNum2 = ymtProductActivityStockWapper.selectByCatalogId(catalogId2).getIstocknum();

			//修改活动限购数量 商品A限购2个
			ProdStockCallService.updateActivityLimit(aid, 0, 2);

			PCatalogListDto catalogListDto1 = new PCatalogListDto();
			catalogListDto1.setProductId(productId1);
			catalogListDto1.setCatalogId(catalogId1);
			catalogListDto1.setSellerId(sellerId);
			catalogListDto1.setActivityId(aid);
			catalogListDto1.setBuyCount(reduceNum1+1);

			PCatalogListDto catalogListDto2 = new PCatalogListDto();
			catalogListDto2.setProductId(productId2);
			catalogListDto2.setCatalogId(catalogId2);
			catalogListDto2.setSellerId(sellerId);
			catalogListDto2.setActivityId(aid);
			catalogListDto2.setBuyCount(reduceNum2);

//			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, catalogId, sellerId, 0, stockNum, 0, true);


//			List<ProductAllPriceBean> productAllPriceBeanList = ProductPriceCallService.getProdPriceListWithActivity_NewBuyerAndNoIsolation(3383,2);
//			//商品A
//			String productId1 = productAllPriceBeanList.get(0).getProductId();
//			productlist.add(productId1);
//			YmtCatalogsI ymtCatalog1 = ymtCatalogsIWapper.selectCatalogsByProductId(productId1).get(0);
//			String catalogId1 = ymtCatalog1.getsCatalogId();
//			int catalogNum1 = ymtCatalog1.getiNum();
//			int reduceNum1 = 1;
//			int actNum1 = ymtProductActivityStockWapper.selectByCatalogId(catalogId1).getIstocknum();
//
//			//商品B
//			String productId2 = productAllPriceBeanList.get(1).getProductId();
//			productlist.add(productId2);
//			YmtCatalogsI ymtCatalog2 = ymtCatalogsIWapper.selectCatalogsByProductId(productId2).get(0);
//			String catalogId2 = ymtCatalog2.getsCatalogId();
//			int catalogNum2 = ymtCatalog2.getiNum();
//			int reduceNum2 = 1;
//			int actNum2 = ymtProductActivityStockWapper.selectByCatalogId(catalogId2).getIstocknum();
//
//			//修改活动限购数量 商品限购2个
//			int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId1).get("aid").toString());
//			ProdStockCallService.updateActivityLimit(aid, 0, 2);
//
//			////////////////////////////////////////////////////////////////////////////////////////////////////
//			// 超卖 扣库存失败
//			PCatalogListDto catalogListDto1 = new PCatalogListDto();
//			catalogListDto1.setProductId(productId1);
//			catalogListDto1.setCatalogId(catalogId1);
//			catalogListDto1.setSellerId(sellerId);
//			catalogListDto1.setActivityId(aid);
//			catalogListDto1.setBuyCount(reduceNum1+1);
//
//			PCatalogListDto catalogListDto2 = new PCatalogListDto();
//			catalogListDto2.setProductId(productId2);
//			catalogListDto2.setCatalogId(catalogId2);
//			catalogListDto2.setSellerId(sellerId);
//			catalogListDto2.setActivityId(aid);
//			catalogListDto2.setBuyCount(reduceNum2);
//
//			ProdStockCallService.ReduceStockListForPreOrder(Arrays.asList(catalogListDto1,catalogListDto2), false, false);
//			// check
//			ProdStockCallService.checkReduceStock(catalogListDto1, catalogNum1, actNum1,false);
//			ProdStockCallService.checkReduceStock(catalogListDto2, catalogNum2, actNum2,false);
//
//			////////////////////////////////////////////////////////////////////////////////////////////////////
//			// 扣库存
//			PCatalogListDto catalogListDto3 = new PCatalogListDto();
//			catalogListDto3.setProductId(productId1);
//			catalogListDto3.setCatalogId(catalogId1);
//			catalogListDto3.setSellerId(sellerId);
//			catalogListDto3.setActivityId(aid);
//			catalogListDto3.setBuyCount(reduceNum1);
//
//			PCatalogListDto catalogListDto4 = new PCatalogListDto();
//			catalogListDto4.setProductId(productId2);
//			catalogListDto4.setCatalogId(catalogId2);
//			catalogListDto4.setSellerId(sellerId);
//			catalogListDto4.setActivityId(aid);
//			catalogListDto4.setBuyCount(reduceNum2);
//
//			ProdStockCallService.ReduceStockListForPreOrder(Arrays.asList(catalogListDto3,catalogListDto4), true, false);
//			// check
//			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum1, actNum1-reduceNum1);
//			ProdStockCallService.checkReduceStock(catalogListDto4, catalogNum2, actNum2-reduceNum2);
//
//			////////////////////////////////////////////////////////////////////////////////////////////////////
//			//多个订单返
//			CatalogStockListDto catalogStockListDto1 = new OrderReturnCatalogDto.CatalogStockListDto();
//			catalogStockListDto1.setBuyerId(catalogListDto3.getBuyerId());
//			catalogStockListDto1.setOrderId(catalogListDto3.getPreOrderId());
//			catalogStockListDto1.setProductId(catalogListDto3.getProductId());
//			catalogStockListDto1.setCatalogId(catalogListDto3.getCatalogId());
//			catalogStockListDto1.setActivityId(catalogListDto3.getActivityId());
//			catalogStockListDto1.setSellerId(sellerId);
//			catalogStockListDto1.setStockNum(catalogListDto3.getBuyCount());
//
//			CatalogStockListDto catalogStockListDto2 = new OrderReturnCatalogDto.CatalogStockListDto();
//			catalogStockListDto2.setBuyerId(catalogListDto4.getBuyerId());
//			catalogStockListDto2.setOrderId(catalogListDto4.getPreOrderId());
//			catalogStockListDto2.setProductId(catalogListDto4.getProductId());
//			catalogStockListDto2.setCatalogId(catalogListDto4.getCatalogId());
//			catalogStockListDto2.setActivityId(catalogListDto4.getActivityId());
//			catalogStockListDto2.setSellerId(sellerId);
//			catalogStockListDto2.setStockNum(catalogListDto4.getBuyCount());
//
//
//			ProdStockCallService.ReturnProductStockForOrder(Arrays.asList(catalogStockListDto1,catalogStockListDto2));
//			//check
//			ProdStockCallService.checkReturnStock(catalogStockListDto1, catalogNum1, actNum1, true);
//			ProdStockCallService.checkReturnStock(catalogStockListDto2, catalogNum2, actNum2, true);
//
//			//幂等
//			ProdStockCallService.ReturnProductStockForOrder(Arrays.asList(catalogStockListDto1,catalogStockListDto2),true,true);
//			ProdStockCallService.checkReturnStock(catalogStockListDto1, catalogNum1, actNum1, true);
//			ProdStockCallService.checkReturnStock(catalogStockListDto2, catalogNum2, actNum2, true);
//
//			////////////////////////////////////////////////////////////////////////////////////////////////////
//			//在下单 //重新生成订单号
//			catalogListDto3.setPreOrderId(0);
//			catalogListDto4.setPreOrderId(0);
//			ProdStockCallService.ReduceStockListForPreOrder(Arrays.asList(catalogListDto3,catalogListDto4), true, false);
//			// check
//			ProdStockCallService.checkReduceStock(catalogListDto3, catalogNum1, actNum1-reduceNum1);
//			ProdStockCallService.checkReduceStock(catalogListDto4, catalogNum2, actNum2-reduceNum2);
//
//			//一个一个返
//			catalogStockListDto2.setBuyerId(catalogListDto4.getBuyerId());
//			catalogStockListDto2.setOrderId(catalogListDto4.getPreOrderId());
//			catalogStockListDto2.setProductId(catalogListDto4.getProductId());
//			catalogStockListDto2.setCatalogId(catalogListDto4.getCatalogId());
//			catalogStockListDto2.setActivityId(catalogListDto4.getActivityId());
//			catalogStockListDto2.setSellerId(sellerId);
//			catalogStockListDto2.setStockNum(catalogListDto4.getBuyCount());
//			ProdStockCallService.ReturnProductStockForOrder(Arrays.asList(catalogStockListDto2));
//			//check
//			ProdStockCallService.checkReturnStock(catalogStockListDto2, catalogNum1, actNum1, true);
//
//			//幂等
//			ProdStockCallService.ReturnProductStockForOrder(Arrays.asList(catalogStockListDto2),true,true);
//			ProdStockCallService.checkReturnStock(catalogStockListDto2, catalogNum1, actNum1, true);
//
//			catalogStockListDto1.setBuyerId(catalogListDto3.getBuyerId());
//			catalogStockListDto1.setOrderId(catalogListDto3.getPreOrderId());
//			catalogStockListDto1.setProductId(catalogListDto3.getProductId());
//			catalogStockListDto1.setCatalogId(catalogListDto3.getCatalogId());
//			catalogStockListDto1.setActivityId(catalogListDto3.getActivityId());
//			catalogStockListDto1.setSellerId(sellerId);
//			catalogStockListDto1.setStockNum(catalogListDto3.getBuyCount());
//			ProdStockCallService.ReturnProductStockForOrder(Arrays.asList(catalogStockListDto1));
//			//check
//			ProdStockCallService.checkReturnStock(catalogStockListDto1, catalogNum1, actNum1, true);
//			//幂等
//			ProdStockCallService.ReturnProductStockForOrder(Arrays.asList(catalogStockListDto1),true,true);
//			ProdStockCallService.checkReturnStock(catalogStockListDto1, catalogNum1, actNum1, true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	/**
	 * not find activity
	 * no find preOrder stock
	 */
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_014() {
		Logger.start(true, "活动直播商品,没有限购,返库存");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			
			CreateLiveActivityBean createliveactivityBean = new CreateLiveActivityBean();
			LiveActivityDto LiveActivity=new LiveActivityDto();
			List<ActivityProductDto> ActivityProducts=new ArrayList<>();
			ActivityProductDto activityProductDto = new ActivityProductDto();
			activityProductDto.setProductId(productId);
			activityProductDto.setActivityPrice(7d);
			activityProductDto.setActivityStock(200);
			activityProductDto.setCatalogId(cids.get(0));
			ActivityProducts.add(activityProductDto);
			
			LiveActivity.setActivityProducts(ActivityProducts);
			LiveActivity.setStartTime(String.valueOf(starttime.getTime()));
			LiveActivity.setEndTime(String.valueOf(starttime.getTime()+1*60*1000));
			LiveActivity.setLiveId(createlive.getId());
			LiveActivity.setSellerId(sellerId);
			createliveactivityBean.setLiveActivity(LiveActivity);
			
			createliveactivityBean.getLiveActivity().setActivityProducts(ActivityProducts);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			
			LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
			//直播秒杀活动id
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			Integer LiveActivityProductId = LiveRoomWapper.selectLiveactivityproduct(liveacctivityid, LiveActivity.getActivityProducts().get(0).getProductId()).get(0).getLiveActivityProductId();
			//int aid = Integer.parseInt(activityProductsWapper.getByProdId(productId).get("aid").toString());
			//更改活动时间
			String beginTime = YMTDateUtil.getBeforeOrNextDay(0, YMTDateUtil.YMDTHMS_STRING);
			String endTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
			UpdateProductActivityStockTimeBean updateProductActivityStockTimeBean = ProdStockCallService.initUpdateProductActivityStockTimeBean(sellerId, productId, liveacctivityid, 1, LiveActivityProductId, beginTime, endTime);
			ProdStockCallService.UpdateProductActivityStockTime(updateProductActivityStockTimeBean);
			// 扣库存
			Integer buyCount = 100;
			PreOrderReduceCatalogDto preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(liveacctivityid, cids.get(0), buyCount);
			preOrderReduceCatalogDto.getCatalogList().get(0).setPreOrderId(System.currentTimeMillis());
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			//反库存
			long refundBillNo = getRefundBillNo();
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			ArrayList<OrderReturnCatalogDtoV2> oList = new ArrayList<OrderReturnCatalogDtoV2>();
			OrderReturnCatalogDtoV2 orderReturnCatalogDto = new OrderReturnCatalogDtoV2();
			orderReturnCatalogDto.setRefundBillNo(refundBillNo);
			orderReturnCatalogDto.setBuyerId(buyerId);
			orderReturnCatalogDto.setOrderId(preOrderReduceCatalogDto.getCatalogList().get(0).getPreOrderId());
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setCatalogId(cids.get(0));
			orderReturnCatalogDto.setActivityId(liveacctivityid);
			orderReturnCatalogDto.setSellerId(sellerId);
			orderReturnCatalogDto.setStockNum(10);
			oList.add(orderReturnCatalogDto);
			returnProductStockForOrderBean.setCatalogStockList(oList);
			returnProductStockForOrderCall.setData(returnProductStockForOrderBean);
			returnProductStockForOrderCall.callService();
			Integer getiNum = new YmtCatalogsIWapper().selectCatalogsByCatalogId(cids.get(0)).getiNum();
			Logger.verifyEquals(catalogNum-activityProductDto.getActivityStock(), getiNum, "验证规格库存");
			Integer istocknum = new YmtProductActivityStockWapper().selectByCatalogId(cids.get(0)).getIstocknum();
			Logger.verifyEquals(activityProductDto.getActivityStock()-buyCount+orderReturnCatalogDto.getStockNum(), istocknum, "验证活动库存");
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_015() {
		Logger.start(true, "活动商品,没有限购,返库存");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);
			Integer istocknum = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			//更改活动时间
			String beginTime = YMTDateUtil.getBeforeOrNextDay(0, YMTDateUtil.YMDTHMS_STRING);
			String endTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
			UpdateProductActivityStockTimeBean updateProductActivityStockTimeBean = ProdStockCallService.initUpdateProductActivityStockTimeBean(sellerId, productId, activityId, 0, Integer.valueOf(aids.get(0)), beginTime, endTime);
			ProdStockCallService.UpdateProductActivityStockTime(updateProductActivityStockTimeBean);
			// 扣库存
			Integer buyCount = 100;
			PreOrderReduceCatalogDto preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(activityId, catalogs.get(0), 100);
			preOrderReduceCatalogDto.getCatalogList().get(0).setPreOrderId(System.currentTimeMillis());
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			//反库存
			long refundBillNo = getRefundBillNo();
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			ArrayList<OrderReturnCatalogDtoV2> oList = new ArrayList<OrderReturnCatalogDtoV2>();
			OrderReturnCatalogDtoV2 orderReturnCatalogDto = new OrderReturnCatalogDtoV2();
			orderReturnCatalogDto.setRefundBillNo(refundBillNo);
			orderReturnCatalogDto.setBuyerId(buyerId);
			orderReturnCatalogDto.setOrderId(preOrderReduceCatalogDto.getCatalogList().get(0).getPreOrderId());
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setCatalogId(catalogs.get(0));
			orderReturnCatalogDto.setActivityId(activityId);
			orderReturnCatalogDto.setSellerId(sellerId);
			orderReturnCatalogDto.setStockNum(10);
			oList.add(orderReturnCatalogDto);
			returnProductStockForOrderBean.setCatalogStockList(oList);
			returnProductStockForOrderCall.setData(returnProductStockForOrderBean);
			returnProductStockForOrderCall.callService();
			Integer getiNum = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Logger.verifyEquals(catalogNum-istocknum, getiNum, "验证规格库存");
			Integer istocknum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Logger.verifyEquals(istocknum-buyCount+orderReturnCatalogDto.getStockNum(), istocknum2, "验证活动库存");
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
		
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_016() {
		Logger.start(true, "普通商品,返库存");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			// 扣库存
			Integer buyCount = 100;
			PreOrderReduceCatalogDto preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(0, catalogs.get(0), 100);
			preOrderReduceCatalogDto.getCatalogList().get(0).setPreOrderId(System.currentTimeMillis());
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			//反库存
			long refundBillNo = getRefundBillNo();
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			ArrayList<OrderReturnCatalogDtoV2> oList = new ArrayList<OrderReturnCatalogDtoV2>();
			OrderReturnCatalogDtoV2 orderReturnCatalogDto = new OrderReturnCatalogDtoV2();
			orderReturnCatalogDto.setRefundBillNo(refundBillNo);
			orderReturnCatalogDto.setBuyerId(buyerId);
			orderReturnCatalogDto.setOrderId(preOrderReduceCatalogDto.getCatalogList().get(0).getPreOrderId());
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setCatalogId(catalogs.get(0));
			orderReturnCatalogDto.setActivityId(0);
			orderReturnCatalogDto.setSellerId(sellerId);
			orderReturnCatalogDto.setStockNum(10);
			oList.add(orderReturnCatalogDto);
			returnProductStockForOrderBean.setCatalogStockList(oList);
			returnProductStockForOrderCall.setData(returnProductStockForOrderBean);
			returnProductStockForOrderCall.callService();
			Integer getiNum = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Logger.verifyEquals(catalogNum-buyCount+orderReturnCatalogDto.getStockNum(), getiNum, "验证规格库存");
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
		
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_017() {
		Logger.start(true, "普通商品,已删除规格，不能扣减库存");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			 //已删除
			CIWapper.updateiActionByCatalogId(-1, catalogs.get(0));
			// 扣库存
			Integer buyCount = 100;
			PreOrderReduceCatalogDto preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(0, catalogs.get(0), 100);
			preOrderReduceCatalogDto.getCatalogList().get(0).setPreOrderId(System.currentTimeMillis());
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			//反库存
			long refundBillNo = getRefundBillNo();
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			ArrayList<OrderReturnCatalogDtoV2> oList = new ArrayList<OrderReturnCatalogDtoV2>();
			OrderReturnCatalogDtoV2 orderReturnCatalogDto = new OrderReturnCatalogDtoV2();
			orderReturnCatalogDto.setRefundBillNo(refundBillNo);
			orderReturnCatalogDto.setBuyerId(buyerId);
			orderReturnCatalogDto.setOrderId(preOrderReduceCatalogDto.getCatalogList().get(0).getPreOrderId());
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setCatalogId(catalogs.get(0));
			orderReturnCatalogDto.setActivityId(0);
			orderReturnCatalogDto.setSellerId(sellerId);
			orderReturnCatalogDto.setStockNum(10);
			oList.add(orderReturnCatalogDto);
			returnProductStockForOrderBean.setCatalogStockList(oList);
			returnProductStockForOrderCall.setData(returnProductStockForOrderBean);
			returnProductStockForOrderCall.callService();
			Integer getiNum = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Logger.verifyEquals(catalogNum, getiNum, "验证规格库存");
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_018() {
		Logger.start(true, "活动商品,活动限购,扣减库存,返库存,扣库存");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);
			Integer istocknum = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			//更改活动时间
			String beginTime = YMTDateUtil.getBeforeOrNextDay(0, YMTDateUtil.YMDTHMS_STRING);
			String endTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
			UpdateProductActivityStockTimeBean updateProductActivityStockTimeBean = ProdStockCallService.initUpdateProductActivityStockTimeBean(sellerId, productId, activityId, 0, Integer.valueOf(aids.get(0)), beginTime, endTime);
			ProdStockCallService.UpdateProductActivityStockTime(updateProductActivityStockTimeBean);
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(activityId, 0, 300);
			// 扣库存
			Integer buyCount = 100;
			PreOrderReduceCatalogDto preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(activityId, catalogs.get(0), 100);
			preOrderReduceCatalogDto.getCatalogList().get(0).setPreOrderId(System.currentTimeMillis());
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			//反库存
			long refundBillNo = getRefundBillNo();
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			ArrayList<OrderReturnCatalogDtoV2> oList = new ArrayList<OrderReturnCatalogDtoV2>();
			OrderReturnCatalogDtoV2 orderReturnCatalogDto = new OrderReturnCatalogDtoV2();
			orderReturnCatalogDto.setRefundBillNo(refundBillNo);
			orderReturnCatalogDto.setBuyerId(buyerId);
			orderReturnCatalogDto.setOrderId(preOrderReduceCatalogDto.getCatalogList().get(0).getPreOrderId());
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setCatalogId(catalogs.get(0));
			orderReturnCatalogDto.setActivityId(activityId);
			orderReturnCatalogDto.setSellerId(sellerId);
			orderReturnCatalogDto.setStockNum(10);
			oList.add(orderReturnCatalogDto);
			returnProductStockForOrderBean.setCatalogStockList(oList);
			returnProductStockForOrderCall.setData(returnProductStockForOrderBean);
			returnProductStockForOrderCall.callService();
			Integer getiNum = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Logger.verifyEquals(catalogNum-istocknum, getiNum, "验证规格库存");
			Integer istocknum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Logger.verifyEquals(istocknum-buyCount+orderReturnCatalogDto.getStockNum(), istocknum2, "验证活动库存");
			//扣库存
			preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(activityId, catalogs.get(0), 100);
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			Logger.verifyEquals(catalogNum-istocknum, getiNum, "验证规格库存");
			Logger.verifyEquals(istocknum-buyCount+orderReturnCatalogDto.getStockNum(), istocknum2, "验证活动库存");
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				 joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_019() {
		Logger.start(true, "活动商品,活动限购,扣减库存,返库存,扣库存");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});

			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			ProductManagerCallServiceV2.approveProductInActivity(productId, aids.get(0), true);
			Integer istocknum = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			//更改活动时间
			String beginTime = YMTDateUtil.getBeforeOrNextDay(0, YMTDateUtil.YMDTHMS_STRING);
			String endTime = YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING);
			UpdateProductActivityStockTimeBean updateProductActivityStockTimeBean = ProdStockCallService.initUpdateProductActivityStockTimeBean(sellerId, productId, activityId, 0, Integer.valueOf(aids.get(0)), beginTime, endTime);
			ProdStockCallService.UpdateProductActivityStockTime(updateProductActivityStockTimeBean);
			//修改活动限购数量 商品限购
			ProdStockCallService.updateActivityLimit(activityId, 0, 300);
			// 扣库存
			Integer buyCount = 100;
			PreOrderReduceCatalogDto preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(activityId, catalogs.get(0), 100);
			preOrderReduceCatalogDto.getCatalogList().get(0).setPreOrderId(System.currentTimeMillis());
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			//反库存
			long refundBillNo = getRefundBillNo();
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			ArrayList<OrderReturnCatalogDtoV2> oList = new ArrayList<OrderReturnCatalogDtoV2>();
			OrderReturnCatalogDtoV2 orderReturnCatalogDto = new OrderReturnCatalogDtoV2();
			orderReturnCatalogDto.setRefundBillNo(refundBillNo);
			orderReturnCatalogDto.setBuyerId(buyerId);
			orderReturnCatalogDto.setOrderId(preOrderReduceCatalogDto.getCatalogList().get(0).getPreOrderId());
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setCatalogId(catalogs.get(0));
			orderReturnCatalogDto.setActivityId(activityId);
			orderReturnCatalogDto.setSellerId(sellerId);
			orderReturnCatalogDto.setStockNum(10);
			oList.add(orderReturnCatalogDto);
			returnProductStockForOrderBean.setCatalogStockList(oList);
			returnProductStockForOrderCall.setData(returnProductStockForOrderBean);
			returnProductStockForOrderCall.callService();
			Integer getiNum = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Logger.verifyEquals(catalogNum-istocknum, getiNum, "验证规格库存");
			Integer istocknum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Logger.verifyEquals(istocknum-buyCount+orderReturnCatalogDto.getStockNum(), istocknum2, "验证活动库存");
			//扣库存
			preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(activityId, catalogs.get(0), 100);
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			Logger.verifyEquals(catalogNum-istocknum, getiNum, "验证规格库存");
			Logger.verifyEquals(istocknum-buyCount+orderReturnCatalogDto.getStockNum(), istocknum2, "验证活动库存");
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * 微信代购神器
	 */

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_022() {
		Logger.start(true, "微信代购商品,渠道商,扣,返库存");
		try{
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			Integer catalogNum = 10000;
			addAgentProductBean.getCatalogList().forEach(p->{
				p.setStock(catalogNum);
			});
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			int reduceNum = 10;
			List<YmtCatalogsI> ymtCatalogsIList = ymtCatalogsIWapper.selectCatalogsByProductId(productId);
			// 扣库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(productId, ymtCatalogsIList.get(0).getsCatalogId(), rootUserId, 0, reduceNum, 0, true);

			//扣根规格库存，代理库存不变
			catalogListDto.setCatalogId(ymtCatalogsIList.get(0).getsCatalogId());
			ProdStockCallService.checkReduceStock(catalogListDto, catalogNum - reduceNum, 0);

			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(productId);
			catalogStockListDto.setCatalogId(ymtCatalogsIList.get(0).getsCatalogId());
			catalogStockListDto.setSellerId(rootUserId);
			catalogStockListDto.setStockNum(reduceNum+1);
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			catalogStockListDtoNew.setCatalogId(ymtCatalogsIList.get(0).getsCatalogId());
			//check
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum+1, 0, true);

		}catch(Exception e){
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_020() {
		Logger.start(true, "微信代购商品,二级代理,扣,返库存");
		try{
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,true);
			Integer catalogNum = 10000;
			addAgentProductBean.getCatalogList().forEach(p->{
				p.setStock(catalogNum);
			});
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,rootUserId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			int reduceNum = 10;
			List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(agentProductId);
			Logger.comment("扣库存");
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(agentProductId, ymtCatalogsIList.get(0).getsCatalogId(), agentUserId1, 0, reduceNum, 0, true);

			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(agentProductId);
			catalogStockListDto.setCatalogId(ymtCatalogsIList.get(0).getsCatalogId());
			catalogStockListDto.setSellerId(agentUserId1);
			catalogStockListDto.setStockNum(reduceNum+1);
			Logger.comment("返库存");
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//验证代理商库存
			Logger.comment("验证代理商库存");
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, 0, 0, true);

			catalogStockListDtoNew.setCatalogId(ymtCatalogsIList.get(0).getParentCatalogId());
			catalogStockListDtoNew.setSellerId(rootUserId);
			catalogStockListDtoNew.setProductId(productId);
			//验证渠道商库存
			Logger.comment("验证渠道商库存");
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum+1, 0, true);

		}catch(Exception e){
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_021() {
		Logger.start(true, "微信代购商品，三级代理,扣,返库存");
		try{
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
			Integer catalogNum = 10000;
			addAgentProductBean.getCatalogList().forEach(p->{
				p.setStock(catalogNum);
			});
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,true);
			importAgentProductBean.setSetAgentPrice(true);
			//渠道商->代理
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId2,vipAgentUserName2,agentProductId,vipAgentUserId1,false);
			//渠道商->代理->代理
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);
			int reduceNum = 10;
			List<YmtCatalogsI> ymtCatalogsIList = new YmtCatalogsIWapper().selectCatalogsByProductId(agentProductId1);
			//扣减库存
			PCatalogListDto catalogListDto = ProdStockCallService.ReduceStockListForPreOrder(agentProductId1, ymtCatalogsIList.get(0).getsCatalogId(), vipAgentUserId2, 0, reduceNum, 0, true);
			//验证代理商2库存为0
			ProdStockCallService.checkReduceStock(catalogListDto, 0, 0);
			//代理商2的规格
			YmtCatalogsI agentYmtCatalog2 = new YmtCatalogsIWapper().selectCatalogsByCatalogId(ymtCatalogsIList.get(0).getsCatalogId());
			catalogListDto.setProductId(agentProductId);
			catalogListDto.setCatalogId(agentYmtCatalog2.getsCatalogId());
			catalogListDto.setSellerId(vipAgentUserId1);
			//验证代理商1的库存
			ProdStockCallService.checkReduceStock(catalogListDto, 0, 0);
			YmtCatalogsI agentYmtCatalog = new YmtCatalogsIWapper().selectCatalogsByCatalogId(ymtCatalogsIList.get(0).getRootCatalogId());
			catalogListDto.setProductId(productId);
			catalogListDto.setCatalogId(agentYmtCatalog.getsCatalogId());
			catalogListDto.setSellerId(vipRootUserId);
			//验证渠道商的库存
			ProdStockCallService.checkReduceStock(catalogListDto,catalogNum-reduceNum, 0);

			//反库存
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDto = new OrderReturnCatalogDto.CatalogStockListDto();
			catalogStockListDto.setBuyerId(catalogListDto.getBuyerId());
			catalogStockListDto.setOrderId(catalogListDto.getPreOrderId());
			catalogStockListDto.setProductId(agentProductId1);
			catalogStockListDto.setCatalogId(ymtCatalogsIList.get(0).getsCatalogId());
			catalogStockListDto.setSellerId(vipAgentUserId2);
			catalogStockListDto.setStockNum(reduceNum+1);
			Logger.comment("返库存");
			OrderReturnCatalogDto.CatalogStockListDto catalogStockListDtoNew = ProdStockCallService.ReturnProductStockForOrder(catalogStockListDto);
			//验证代理商库存
			Logger.comment("验证代理商2库存");
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, 0, 0, true);

			Logger.comment("验证代理商1库存");
			catalogStockListDtoNew.setProductId(agentProductId);
			catalogStockListDtoNew.setSellerId(vipAgentUserId1);
			catalogStockListDtoNew.setCatalogId(ymtCatalogsIList.get(0).getParentCatalogId());
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, 0, 0, true);


			Logger.comment("验证渠道商库存");
			catalogStockListDtoNew.setProductId(productId);
			catalogStockListDtoNew.setSellerId(vipRootUserId);
			catalogStockListDtoNew.setCatalogId(ymtCatalogsIList.get(0).getRootCatalogId());
			ProdStockCallService.checkReturnStock(catalogStockListDtoNew, catalogNum+1, 0, true);


		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_ReturnProductStockForOrder_023() {
		Logger.start(true, "活动商品,活动限购,扣减库存,返库存,扣库存  查询redis");
		try {
			//创建商品
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			// 设置商品库存,规格库存
			Integer catalogNum = 1000;
			addpro.getProduct().getCatalogList().forEach(catalog -> {
				catalog.setStock(catalogNum);
			});
			//获取商品ID
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			//获取规格ID
			List<String> catalogs = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			System.out.println(catalogs.toString());
			//创建一个活动  得到活动id
			Integer activityId = ProductManagerCallServiceV2.createIsolationActivityInProcess();
			//报名活动  获得商品在活动中的id
			List<String> aids = ProductManagerCallServiceV2.ApplyActivity(productId, sellerId,catalogs,activityId, 100,"10");
			//添加秒杀商品数据
			ProductManagerCallServiceV2.addSecKillStock(activityId,productId, sellerId, true);
			//审核通过
			ProductManagerCallServiceV2.approveProductInActivity(productId,aids.get(0),true);
			Integer istocknum = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			RedisDriver rd = new RedisDriver("group1",EnvSetup.getSystemUrl("sentinel"));
			String key="SecStock:"+aids.get(0)+":"+catalogs.get(0);
			int stock=Integer.parseInt(rd.getValue(key));
			Logger.comment("获取redis，key为："+key+"值为："+rd.getValue(key));
			// 扣库存
			Integer buyCount = 10;
			PreOrderReduceCatalogDto preOrderReduceCatalogDto = ProdStockCallService.initPreOrderReduceCatalogDto(activityId, catalogs.get(0), buyCount);
			preOrderReduceCatalogDto.getCatalogList().get(0).setPreOrderId(System.currentTimeMillis());
			ProdStockCallService.ReduceStockListForPreOrder(preOrderReduceCatalogDto);
			int stock1=Integer.parseInt(rd.getValue(key));
			Logger.comment("获取redis，key为："+key+"值为："+rd.getValue(key));
			//反库存
			long refundBillNo = getRefundBillNo();
			int buyerId = Math.abs(TestDataManager.getRandomIntNum(4));
			ArrayList<OrderReturnCatalogDtoV2> oList = new ArrayList<OrderReturnCatalogDtoV2>();
			OrderReturnCatalogDtoV2 orderReturnCatalogDto = new OrderReturnCatalogDtoV2();
			orderReturnCatalogDto.setRefundBillNo(refundBillNo);
			orderReturnCatalogDto.setBuyerId(buyerId);
			orderReturnCatalogDto.setOrderId(preOrderReduceCatalogDto.getCatalogList().get(0).getPreOrderId());
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setCatalogId(catalogs.get(0));
			orderReturnCatalogDto.setActivityId(activityId);
			orderReturnCatalogDto.setSellerId(sellerId);
			orderReturnCatalogDto.setStockNum(buyCount);
			oList.add(orderReturnCatalogDto);
			returnProductStockForOrderBean.setCatalogStockList(oList);
			returnProductStockForOrderCall.setData(returnProductStockForOrderBean);
			returnProductStockForOrderCall.callService();
			
			int stock2=Integer.parseInt(rd.getValue(key));
			Logger.comment("获取redis，key为："+key+"值为："+rd.getValue(key));
			//发送同步指令SyncActivityProduct
			MessageListBean MessageListBean = MqQueryCallService.initPMongoCrud(productId);
			List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
			if(joa.size()==0){
				Thread.sleep(5000);
				joa = MqQueryCallService.GetMqMessage(MessageListBean);
			}
			joa.forEach(ele->{
				Logger.debug(ele.toString());
			});
			Integer getiNum = new YmtCatalogsIWapper().selectCatalogsByCatalogId(catalogs.get(0)).getiNum();
			Logger.verifyEquals(catalogNum-istocknum, getiNum, "验证规格库存");
			Integer istocknum2 = new YmtProductActivityStockWapper().selectByCatalogId(catalogs.get(0)).getIstocknum();
			Logger.verifyEquals(istocknum-buyCount+orderReturnCatalogDto.getStockNum(), istocknum2, "验证活动库存");
			Logger.verifyEquals(stock, stock2, "");
			Logger.verifyNotEquals(stock, stock1, "");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public static long getRefundBillNo(){
		return Long.parseLong(YMTDateUtil.getSysTime("yyyyMMddHHmmssSSS"));
//		return Long.parseLong(YMTDateUtil.getSysTime("HmmssSSS"));
	}

	public List<CatalogDto> setCatalogList(List<CatalogDto>	catalogDtos, int userId) throws IOException {
		listagentTypeBean listagentTypeBean = GodmchManageCallService.InitlistgentTypeBean(userId);
		List<AgentTypeVo> agentTypeVos = GodmchManageCallService.listgentTypeCall(listagentTypeBean).getAgentTypes();
		catalogDtos.forEach(p -> {
			List<AgentPriceDto> agentPriceDtos1 = new ArrayList<>();
			agentTypeVos.forEach(x -> {
				AgentPriceDto agentPriceDto = new AgentPriceDto();
				agentPriceDto.setAgentTypeId(x.getAgentType());
				double price = Double.parseDouble(1 + TestDataManager.getRandomNum(2));
				agentPriceDto.setAgentPrice(price);
				agentPriceDtos1.add(agentPriceDto);
			});
			p.setAgentPriceList(agentPriceDtos1);
		});
		return catalogDtos;
	}
	 
}
